﻿namespace Chain.IDAL
{
    using Chain.DBUtility;
    using Chain.Model;
    using System;
    using System.Data;
    using System.Data.SqlClient;
    using System.Runtime.InteropServices;
    using System.Text;
    public class Mem_Activity
    {
        public int Add(Chain.Model.Mem_Activity model)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("insert into Mem_Activity(");
            builder.Append("MemID,ActivityID,moeny,score,CouponTitle,PresentsName,Mem_ShareId,Mem_ExtendId,Mem_RebateId,Status,Mem_MoneyRebateId)");
            builder.Append(" values (");
            builder.Append("@MemID,@ActivityID,@money,@score,@CouponTitle,@PresentsName,@Mem_ShareId,@Mem_ExtendId,@Mem_RebateId,1,@Mem_MoneyRebateId)");
            builder.Append(";select @@IDENTITY");
            SqlParameter[] cmdParms = new SqlParameter[] {
                new SqlParameter("@MemID", SqlDbType.Int,4),
                new SqlParameter("@ActivityID", SqlDbType.Int,4),
                new SqlParameter("@money", SqlDbType.Decimal,4),
                new SqlParameter("@score", SqlDbType.Int,4),
                new SqlParameter("@CouponTitle", SqlDbType.NVarChar,500),
                new SqlParameter("@PresentsName", SqlDbType.NVarChar,500),
                new SqlParameter("@Mem_ShareId", SqlDbType.Int,4),
                new SqlParameter("@Mem_ExtendId", SqlDbType.Int,4),
                new SqlParameter("@Mem_RebateId", SqlDbType.Int,4),
            new SqlParameter("@Mem_MoneyRebateId", SqlDbType.Int,4)
             };
            cmdParms[0].Value = model.MemID;
            cmdParms[1].Value = model.ActivityID;
            cmdParms[2].Value = model.moeny;
            cmdParms[3].Value = model.score;
            cmdParms[4].Value = model.CouponTitle;
            cmdParms[5].Value = model.PresentsName;
            cmdParms[6].Value = model.Mem_ShareId;
            cmdParms[7].Value = model.Mem_ExtendId;
            cmdParms[8].Value = model.Mem_RebateId;
            cmdParms[9].Value = model.Mem_MoneyRebateId;

            object single = DbHelperSQL.GetSingle(builder.ToString(), cmdParms);
            if (single == null)
            {
                return 0;
            }
            return Convert.ToInt32(single);
        }



        public bool Exists(int MemID, int ActivityID)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select count(1) from Mem_Activity");
            builder.Append(" where MemID=@MemID and ActivityID=@ActivityID and status=1");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@MemID", SqlDbType.Int, 4), new SqlParameter("@ActivityID", SqlDbType.Int, 4) };
            cmdParms[0].Value = MemID;
            cmdParms[1].Value = ActivityID;
            return DbHelperSQL.Exists(builder.ToString(), cmdParms);
        }

        public DataTable GetList(int MemID) {

            StringBuilder builder = new StringBuilder();
            builder.Append(" select * from Mem_Activity");
            builder.Append(" where MemID=@MemID and Status=1");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@MemID", SqlDbType.Int, 4) };
            cmdParms[0].Value = MemID;
            return DbHelperSQL.Query(builder.ToString(), cmdParms).Tables[0];
        }

        public bool UpdateClose(int MemID)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("update Mem_Activity set ");
            builder.Append("Status=0");
            builder.Append(" where MemID=@MemID and Status=1");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@MemID", SqlDbType.Int, 4) };
            cmdParms[0].Value = MemID;

            return (DbHelperSQL.ExecuteSql(builder.ToString(), cmdParms) > 0);
        }


    }
}
